//
//  OrderAddressCell.m
//  danpin
//
//  Created by chuangjia on 27/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderAddressCell.h"

@interface OrderAddressCell ()
{
    
}
@property(nonatomic,strong)UIImageView* imgView;
@property(nonatomic,strong)UIImageView* imgLocationView;

@property(nonatomic,strong)UILabel *defaultLabel;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *phoneLabel;
@property(nonatomic,strong)UILabel *addressLabel;

@end
@implementation OrderAddressCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mailborder.jpg"]];
        _imgView.frame = CGRectMake(0, 0, DEVICE_WIDTH, 3);
        _imgView.contentMode=UIViewContentModeScaleToFill;
        [self.contentView addSubview:_imgView];
        
        _defaultLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _defaultLabel.backgroundColor = [UIColor clearColor];
        _defaultLabel.textColor = [UIColor darkGrayColor];
        _defaultLabel.textAlignment = NSTextAlignmentLeft;
        _defaultLabel.font = FONT(14);
        [self.contentView addSubview:_defaultLabel];
        _nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.textColor = [UIColor darkGrayColor];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = FONT(14);
        [self.contentView addSubview:_nameLabel];
        _phoneLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _phoneLabel.backgroundColor = [UIColor clearColor];
        _phoneLabel.textColor = [UIColor darkGrayColor];
        _phoneLabel.textAlignment = NSTextAlignmentRight;
        _phoneLabel.font = FONT(14);
        [self.contentView addSubview:_phoneLabel];
        
        _addressLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _addressLabel.backgroundColor = [UIColor clearColor];
        _addressLabel.textColor = [UIColor darkGrayColor];
        _addressLabel.textAlignment = NSTextAlignmentLeft;
        _addressLabel.font = FONT(13);
        [self.contentView addSubview:_addressLabel];
        
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    [self.defaultLabel removeFromSuperview];
    self.defaultLabel=nil;
    
    [self.nameLabel removeFromSuperview];
    self.nameLabel=nil;
    
    [self.phoneLabel removeFromSuperview];
    self.phoneLabel=nil;
    
    [self.addressLabel removeFromSuperview];
    self.addressLabel=nil;
 
    
    if(_imgLocationView)
    {
        [_imgLocationView removeFromSuperview];
        _imgLocationView = nil;
    }
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}

- (UIImageView*) imgLocationView
{
    if(!_imgLocationView)
    {
        _imgLocationView = [[UIImageView alloc]initWithFrame:CGRectMake(_defaultLabel.frame.origin.x - 30,
                                                                        (70 - 20)/2, 20, 20)];
    
        _imgLocationView.image = [UIImage imageNamed:@"location.png"];
        
        [self addSubview:_imgLocationView];
    }
    
    return _imgLocationView;
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    NSString *realname =[dict stringForKey:@"real_name" withDefaultValue:@""];
    if (realname.length==0)
    {
        if ([key isEqualToString:@"OrderDetailAddressCell"])
        {
            _defaultLabel.text =@"";
        }
        else
        {
            _defaultLabel.text =@"添加收件地址...";
        }
    
        _defaultLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH + 40, 0,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 70);
        
        [self imgLocationView].hidden = NO;
    }
    else
    {
        _nameLabel.text =realname;
        CGSize size = [_nameLabel boundingRectWithSize:CGSizeMake(PHOTO_FRAME_WIDTH*10, 20)];
        _nameLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH , PHOTO_FRAME_WIDTH+ _imgView.frame.size.height,size.width, 20);
        //
        _phoneLabel.text =[dict stringForKey:@"phone" withDefaultValue:@""];
        CGSize size1 = [_phoneLabel boundingRectWithSize:CGSizeMake(PHOTO_FRAME_WIDTH*10, 20)];
        _phoneLabel.frame =CGRectMake(_nameLabel.frame.size.width+_nameLabel.frame.origin.x+PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH+ _imgView.frame.size.height,size1.width, 20);
        //
        NSString *provinceStr =[dict stringForKey:@"province" withDefaultValue:@""];
        NSString *cityStr = [dict stringForKey:@"city" withDefaultValue:@""];
        NSString *districtStr =[dict stringForKey:@"town" withDefaultValue:@""];
        NSString *detail = [dict stringForKey:@"detail_address" withDefaultValue:@""];
        NSString *showMsg = [NSString stringWithFormat: @"%@%@%@%@", provinceStr, cityStr, districtStr,detail];
        
        _addressLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, 35,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 20);
        _addressLabel.text =showMsg;
    }

    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return PHOTO_FRAME_WIDTH*6.5;
}

@end
